#include "mainwindow.h"

MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
{
   QTextCodec::setCodecForTr(QTextCodec::codecForName ("UTF-8"));

   createMenus();
   createStatusBar();

   layout = new QVBoxLayout();

   widget = new QWidget;
   widget->setLayout(layout);
   setCentralWidget(widget);

   centralWidget()->setLayout(layout);


   label = new QLabel(tr("Smutna etykieta :("), this);
   button = new QPushButton(tr("&Wciśnij mnie ;)"), this);
   butlab = new QPushButton(tr("&Zmień napis"), this);
   term = new QTextEdit(tr("Konsola"), this);
   term->setReadOnly(1);
   saveb = new QPushButton(tr("Zapisz log"), this);
   spacer = new QSpacerItem(300, 300, QSizePolicy::Expanding);

      layout->addWidget(label);
      layout->addSpacerItem(spacer);
      layout->addWidget(button);
      layout->addWidget(butlab);
      layout->addWidget(term);
      layout->addWidget(saveb);



   connect(button, SIGNAL(clicked()), this, SLOT(listoffile()));
   connect(butlab, SIGNAL(clicked()), this, SLOT(foo()));
   connect(saveb, SIGNAL(clicked()), this, SLOT(savev()));

   setMinimumSize(200, 200);
   resize(480, 320);

   currentDir = QDir("/home/lmviiitest/Projekty/");

}

void MainWindow::listoffile()
{
    Sru please;
    for (int i = 0; i < openfile.size(); ++i) {
        QFile file(openfile[i]);
        qint64 size = QFileInfo(file).size();

        QTableWidgetItem *fileNameItem = new QTableWidgetItem(QFileInfo(file).fileName());
        fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
        QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
                                             .arg(int((size + 1023) / 1024)));
        QTableWidgetItem *directoryt = new QTableWidgetItem(currentDir.relativeFilePath(QFileInfo(file).path()));
        sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
        sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);

        int row = please.tableWidget->rowCount();
        please.tableWidget->insertRow(row);
        please.tableWidget->setItem(row, 0, directoryt);
        please.tableWidget->setItem(row, 1, fileNameItem);
        please.tableWidget->setItem(row, 2, sizeItem);
    }

    if (please.exec()) {

    }
}

void MainWindow::foo()
{

    if (label->text()=="Smutna etykieta :("){
        label->setText(tr("Wesoła etykieta :)"));
        term->append(tr("Etykieta zmieniona na wesołą"));
    }
    else if (label->text()==tr("Wesoła etykieta :)")){
        label->setText(tr("Smutna etykieta :("));
        term->append(tr("Etykieta zmieniona na smutną"));
    };



}

void MainWindow::openfilesv()
{
    term->append(tr("Klik!"));
    openfile = QFileDialog::getOpenFileNames(this,
         tr("Open Dedelus Scripts"), currentDir.path(), tr("Scripts (*.d)"));

    for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it){
    QFileInfo fi;
    fi.setFile(*it);
    term->append(fi.fileName());
    term->append(currentDir.relativeFilePath(fi.path()));

    }
}

void MainWindow::opendirv()
{
    term->append(tr("Open Directory"));

    QString directory_path = QFileDialog::getExistingDirectory(this, tr("Select your music directory"), currentDir.path());
    QDirIterator directory_walker(directory_path, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
    while(directory_walker.hasNext())
    {
        // then we tell our directory_walker object to explicitly take next element until the loop finishes
        directory_walker.next();
        // I want to list just d files!
        if(directory_walker.fileInfo().completeSuffix() == "d")
        {
            openfile << directory_walker.filePath();
        }
    }
    for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it)
    {
        QFileInfo fi;
        fi.setFile(*it);
        term->append(fi.fileName());
        term->append(currentDir.relativeFilePath(fi.path()));
    }
}

void MainWindow::savev()
{
    QFile file("out.txt");
         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
             return;

         QTextStream out(&file);
         out << term->toPlainText();
         term->append(tr("Trach!"));
}

void MainWindow::createMenus()
{
   menu = menuBar()->addMenu(tr("&Plik"));

   openFAction = new QAction(tr("&Add files"),this);
   openFAction->setStatusTip(tr("Add files."));
   connect (openFAction, SIGNAL(triggered()), this, SLOT(openfilesv()));
   menu->addAction(openFAction);

   openDAction = new QAction(tr("&Add directory"),this);
   openDAction->setStatusTip(tr("Add directry."));
   connect (openDAction, SIGNAL(triggered()), this, SLOT(opendirv()));
   menu->addAction(openDAction);

   quitAction = new QAction(tr("&Wyjście"), this);
   quitAction->setStatusTip(tr("Wyjdź z programu."));
   connect (quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
   menu->addAction(quitAction);
}

void MainWindow::createStatusBar()
{
   statusBar()->showMessage(tr("Gotowy"));
}
